import Vue from 'vue';
import Vuex from 'vuex';
import util from '../common/util.js'

Vue.use(Vuex)
//Vuex  数据状态管理不能永久存放，配合缓存
const store = new Vuex.Store({
	state:{
		version:'1.0.0',
		invitees:0,//邀请人id
		coupon:null,
		showLog:false,
		hasLogin:false,//是否登录
		
		c2cotherAtor:'',
		isLogin: false,
		userInfo: null || uni.getStorageSync("userInfo"),
		isSDKReady: false, // TIM SDK 是否 ready
		conversationActive: {}, //聊天进行中的会话
		conversationActiveGroup:{},//群组聊天进行中的会话
		toUserId: '', //聊天对象id
		toGroupId:'',
		conversationList: [], //会话列表
		conversationListGroup:[],
		currentMessageList: [],
		currentMessageListGroup: [],
		requestLoading: false, //加载等待是否显示
		
		groupNewMsg:'',
		relStr:'',//员工加分  员工id字符
		appointObj:null,//指派人员
	},
	mutations: {
	   //更新登录状态
	   toggleIsLogin(state, isLogin) {
	   	state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
	   },
	   //更新TIMSDK状态
	   toggleIsSDKReady(state, isSDKReady) {
	   	state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
	   },
	   //退出登录重置状态
	   reset(state) {
	   	state.isLogin = false
	   	state.isSDKReady = false
	   },
	   //选择好友聊天--创建会话/拼接会话id
	   createConversationActive(state, toUserId) {
	   	state.conversationActive.conversationID = 'C2C' + toUserId
	   	state.toUserId = toUserId
	   	state.currentMessageList = []
	   },
	   //选择群组聊天--创建会话/拼接会话id
	   createConversationActiveGroup(state, toUserId) {
	   	state.conversationActiveGroup.conversationID = 'GROUP' + toUserId
	   	state.toGroupId = toUserId
	   	state.currentMessageListGroup = []
	   },
	   //选择已有会话聊天--更新选中会话详情
	   updateConversationActive(state, conversationItem) {
	   	console.log(state)
	   	console.log(conversationItem)
	   	state.conversationActive = Object.assign({}, conversationItem);
	   	state.toUserId = conversationItem.userProfile.userID;
	   
	   	console.log(conversationItem);
	   	state.currentMessageList = []
	   },
	   //更新会话列表
	   updateConversationList(state, newConversationList) {
	   	state.conversationList = newConversationList
	   },
	   //选择已有会话聊天--更新选中会话详情  群组
	   updateConversationActiveGroup(state, conversationItem) {
	   	console.log(state)
	   	console.log(conversationItem)
	   	state.conversationActiveGroup = Object.assign({}, conversationItem);
	   	state.toGroupId = conversationItem.groupProfile.groupID;
	   
	   	console.log(conversationItem);
	   	state.currentMessageListGroup = []
	   },
	   //更新会话列表  群组
	   // updateConversationListGroup(state, newConversationList) {
	   // 	state.conversationListGroup = newConversationList
	   // },
	   /**
	    * 将消息插入当前会话列表
	    * 调用时机：收/发消息事件触发时
	    * @param {Object} state
	    * @param {Message[]|Message} data
	    * @returns
	    */
	   pushCurrentMessageList(state, data) {
	   	// 还没当前会话，则跳过
	   	if (Array.isArray(data)) {
	   		// 筛选出当前会话的消息
	   		const result = data.filter(item => item.conversationID === state.conversationActive.conversationID)
	   		state.currentMessageList = [...state.currentMessageList, ...result]
	   	} else if (data.conversationID === state.conversationActive.conversationID) {
	   		state.currentMessageList = [...state.currentMessageList, data]
	   	}
	   	console.log(util.getStorage("userInfo"))
	   	console.log('1111')
	   	console.log(state.currentMessageList)
	   },
	   pushCurrentMessageListGroup(state, data) {
	   	// 还没当前会话，则跳过
	   	if (Array.isArray(data)) {
	   		// 筛选出当前会话的消息
	   		const result = data.filter(item => item.conversationID === state.conversationActiveGroup.conversationID)
	   		state.currentMessageListGroup = [...state.currentMessageListGroup, ...result]
	   	} else if (data.conversationID === state.conversationActiveGroup.conversationID) {
	   		state.currentMessageListGroup = [...state.currentMessageListGroup, data]
	   	}
	   	console.log(util.getStorage("userInfo"))
	   	console.log('1111')
	   	console.log(state.currentMessageListGroup)
	   },
	   /**
	    * 滑到顶部请求更多的历史消息
	    * */
	   unshiftCurrentMessageList(state, data) {
	   	console.log(data,'storeUnshift')
	   	if (data) {
	   		state.currentMessageList = [...data, ...state.currentMessageList]
	   	}
	   },
	   unshiftCurrentMessageListGroup(state, data) {
	   	console.log(data)
	   	if (data) {
	   		state.currentMessageListGroup = [...data, ...state.currentMessageListGroup]
	   	}
	   },
	}
})
export default store
